package com.doeis.minaserver.dao;

import java.util.ArrayList;
import java.util.List;

import com.google.inject.Singleton;
import com.voland.core.db.Dao;
import com.voland.core.db.DaoException;
import com.voland.core.db.Page;

@Singleton
public class UserDao extends Dao {

	public User findUserByid(Object id) throws DaoException{
		try{
			return super.readById(User.class, id);
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	public User findUserByid(Object id,boolean doAssociate) throws DaoException{
		try{
			return super.readById(User.class, id,doAssociate);
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	public User findUserForLogin(String loginid, boolean isVps) throws DaoException{
		try{
			if(isVps){
				return super.readByCondition(User.class, "(login =? or email = ? or mobile = ?) and status = true", true, loginid, loginid, loginid );
			}else{
				return super.readByCondition(User.class, "(login =? or email = ?) and status = true ", true, loginid, loginid);
			}
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}

	public User findUserByLogin(String login) throws DaoException{
		try{
			return super.readByCondition(User.class, "login =? and status = true", true, login);
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	public User findUserByEmail(String email) throws DaoException{
		try{
			return super.readByCondition(User.class, "email =? and status = true", email);
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	public User findUserByMobile(String mobile) throws DaoException{
		try{
			return super.readByCondition(User.class, "mobile =? and status = true", mobile);
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	public List<User> findUserByMobile(String mobile,String switchboard_id) throws DaoException{
		try{
			return super.query(User.class, "select * from user where mobile =?  and switchboard_id = ? and status = true ", mobile, switchboard_id);
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	public User findUserByName(String name) throws DaoException{
		try{
			return super.readByCondition(User.class, "name =? and status = true", name);
		}catch (Exception e) {
			throw new DaoException(e);
		}
	}
	
	
	public Page<User> listInnerUsers(Page page , User userForm) throws DaoException{
		try {
			List<Object> params = new ArrayList<Object>();
			StringBuffer sql =  new StringBuffer("select * from user where status = true ");
			
			if(userForm != null){
				sql.append(" and switchboard_id = ? ");
				params.add( userForm.getSwitchboard_id() );			
			}		
			return super.querySlice(User.class, sql.toString(), page, true, params.toArray());
		} catch (Exception e) {
			throw new DaoException(e);
		}
		
	}
		
	public List<User> findUserBySwitchBoard(String switchboard_id,boolean doAssociate) {
		StringBuffer sql = new StringBuffer("select * from user where switchboard_id=? and status = true");
		return super.query(User.class, sql.toString(), doAssociate, switchboard_id);
	}

	public List<User> findUserByDepartmentId(String departmentId,boolean doAssociate) {
		StringBuffer sql = new StringBuffer("select * from user where department_id=? and status = true");
		return super.query(User.class, sql.toString(), doAssociate, departmentId);
	}
	
	public List<User> findUserByRoleId(String roleId,boolean doAssociate) {
		StringBuffer sql = new StringBuffer("select * from user where role_id=? and status = true");
		return super.query(User.class, sql.toString(), doAssociate, roleId);
	}
	
}